<template>
    <div>
      <h1>商品秒杀</h1>
      <div v-for="item in items" :key="item.id">
        <h2>{{ item.name }}</h2>
        <p>库存: {{ item.stock }}</p>
        <button @click="seckill(item.id)" :disabled="item.stock <= 0">立即抢购</button>
      </div>
    </div>
  </template>
  
  <script>
  import axios from 'axios';
  import {fetchItems, seckill} from '@/api/seckill';
  
  export default {
    data() {
      return {
        items: []
      };
    },
    created() {
      this.fetchItems();
    },
    methods: {
      fetchItems() {
        fetchItems().then(response => {
          this.items = response.data;
        });
      },
      seckill(itemId) {
        let param = {
            itemId:itemId,
            userId:"10001"
        };
        seckill(param).then(response => {
          if (response.data.success) {
            alert('抢购成功');
            this.fetchItems();
          } else {
            alert('抢购失败: ' + response.data.message);
          }
        });
      }
    }
  };
  </script>